package com.business.user.service.impl;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.business.common.dto.R;
import com.business.common.enums.BusinessExceptionEnum;
import com.business.common.exception.BusinessException;
import com.business.common.utils.StrKit;
import com.business.user.entity.UserDetails;
import com.business.user.mapper.UserDetailsMapper;
import com.business.user.mapper.UserMapper;
import com.business.user.service.UserTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * 用户标签表:业务接口实现类
 *
 * @author chenjiehong
 * @date 2019-11-29 14:28:27
 * @since 1.0
 */
@Service
public class UserTagServiceImpl implements UserTagService {

    @Autowired
    private UserDetailsMapper userDetailsMapper;

    @Override
    @Transactional
    public Integer add(Integer userId, String tag) {
        if (tag.length() > 10) {
            throw new BusinessException(BusinessExceptionEnum.USER_TAG_LENGTH_ERROR);
        }
        String tags = userDetailsMapper.getTags(userId);
        if (StrKit.isNotEmpty(tags)) {
            String[] tagList = tags.split(",");
            if (tagList.length >= 5) {
                throw new BusinessException(BusinessExceptionEnum.USER_TAG_COUNT_ERROR);
            }
            if (tags.indexOf(tag) > -1) {
                throw new BusinessException(BusinessExceptionEnum.USER_TAG_EXIST);
            }
        }
        UserDetails userDetails = new UserDetails();
        userDetails.setUserId(userId);
        if (StrKit.isNotEmpty(tags)) {
            userDetails.setTags(tags + "," + tag);
        } else {
            userDetails.setTags(tag);
        }
        return  userDetailsMapper.updateById(userDetails);
    }

    @Override
    @Transactional
    public Integer addList(Integer userId, List<String> tagList) {
        tagList.forEach(tag -> {
            add(userId, tag);
        });
        return 1;
    }


    @Override
    public String[] queryTagList(Integer userId) {
        String tags = userDetailsMapper.getTags(userId);
        if (StrKit.isNotEmpty(tags)) {
            return tags.split(",");
        }
        return new String[0];
    }

    @Override
    @Transactional
    public Integer delete(Integer userId, String tag) {
        String tags = userDetailsMapper.getTags(userId);
        int index = tags.indexOf(tag);
        if (index == -1) {
            throw new BusinessException(BusinessExceptionEnum.USER_TAG_NOT_EXIST);
        }
        if (index == 0) {
            if (tags.length() > tag.length()) {
                tags = tags.substring(tag.length() + 1);
            } else {
                tags = "";
            }
        } else {
            tags = tags.replace("," + tag, "");
        }
        UserDetails userDetails = new UserDetails();
        userDetails.setUserId(userId);
        userDetails.setTags(tags);
        return userDetailsMapper.updateById(userDetails);
    }

    @Override
    @Transactional
    public Integer deleteList(Integer userId, List<String> tags) {
        tags.forEach(tag -> {
            delete(userId, tag);
        });
        return 1;
    }


}